package file;
class Animal {
    String name;
    int age;

    public Animal(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void eat(){
        System.out.println(name + "吃饭");
    }
}

class Cat extends Animal{
    public Cat(String name, int age){
        super(name, age);
    }

    @Override
    public void eat(){
        System.out.println(name+"吃鱼~~~");
    }

    public void run(){
        System.out.println(name+"跑");
    }
}

class Dog extends Animal {
    public Dog(String name, int age){
        super(name, age);
    }

    @Override
    public void eat(){
        System.out.println(name+"吃骨头~~~");
    }

    public void swim(){
        System.out.println(name+"游泳");
    }
}

public class TestAnimal {

    public static void main(String[] args) {
        Cat cat = new Cat("猫猫", 10);
        Dog dog = new Dog("旺旺", 6);

        // 向上转型
        Animal animal = cat;
        animal.eat();

        animal = dog;
        animal.eat();

        if (animal instanceof Cat) {
            cat = (Cat)animal;
            cat.run();
        }
        if (animal instanceof Dog) {
            dog = (Dog) animal;
            dog.swim();
        }
    }
}
